package com.theatre.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theatre.userservice.mapper.ScriptMapper;
import com.theatre.userservice.model.entity.Script;
import com.theatre.userservice.service.ScriptService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ScriptServiceImpl extends ServiceImpl <ScriptMapper, Script> implements ScriptService {
    /**
     * 标记已到期剧本
     */
    @Override
    public int expireScripts() {
        // 构建更新条件：expire_time <= now 且 status = 0
        UpdateWrapper<Script> wrapper = new UpdateWrapper<>();
        wrapper.le("expire_time", LocalDateTime.now());

        // 构建更新内容
        Script script = new Script();
        script.setStatus(1);        // 标记已到期
        script.setSold(0);          // 清空售出数量
        script.setShopId(null);     // 清空所属商铺
        script.setExpireTime(null); // 清空过期时间

        // 执行更新
        boolean success = this.update(script, wrapper);
        // 返回受影响行数
        return success ? 1 : 0;
    }
}
